package com.yy.personalcv.controller;

import com.yy.personalcv.pojo.Skill;
import com.yy.personalcv.pojo.UserInformation;
import com.yy.personalcv.service.SkillService;
import com.yy.personalcv.tool.StringUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class SkillController {

    @Resource
    private SkillService skillService;

    @RequestMapping(value = "/skillList.do")
    public String getSkillList(HttpServletRequest request, Model model,
                               @RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "5") int size){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        int uid = userInformation.getId();
        int counts = getSkillCounts(uid);
        List<Skill> skills = selectTen(page, size, uid);
        model.addAttribute("skills", skills);
        model.addAttribute("page", page);
        model.addAttribute("size", skills.size());
        model.addAttribute("max_size", size);
        model.addAttribute("counts", counts);
        model.addAttribute("userInformation", userInformation);
        return "skill_page";
    }

    @RequestMapping(value = "/addSkill.do")
    public String addSkill(HttpServletRequest request, Model model,
                           @RequestParam String name, @RequestParam String description){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(name) || StringUtils.getInstance().isNullOrEmpty(description)){
            log.error("name or description is null");
            return "redirect:/skillList.do";
        }
        int uid = userInformation.getId();
        Skill skill = new Skill();
        skill.setName(name);
        skill.setDescription(description);
        skill.setUid(uid);
        skillService.addSkill(skill);
        model.addAttribute("userInformation", userInformation);
        return "redirect:/skillList.do";
    }

    @RequestMapping(value = "/editSkill.do")
    public String editSkill(HttpServletRequest request, Model model,
                            @RequestParam String id, @RequestParam String name, @RequestParam String description){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(id) || StringUtils.getInstance().isNullOrEmpty(name) || StringUtils.getInstance().isNullOrEmpty(description)){
            log.error("id or name or description is null");
            return "redirect:/skillList.do";
        }
        int uid = userInformation.getId();
        Skill skill = new Skill();
        skill.setId(Integer.parseInt(id));
        skill.setName(name);
        skill.setDescription(description);
        skill.setUid(uid);
        skillService.editSkill(skill);
        model.addAttribute("userInformation", userInformation);
        return "redirect:/skillList.do";
    }

    @RequestMapping(value = "/skillDelete.do")
    public String deleteSkill(HttpServletRequest request, Model model,
                              @RequestParam int id){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(id)){
            log.error("id is null");
            return "redirect:/skillList.do";
        }
        int uid = userInformation.getId();
        skillService.deleteSkill(id, uid);
        model.addAttribute("userInformation", userInformation);
        return "redirect:/skillList.do";
    }

    private List<Skill> selectTen(int page, int size, int uid){
        Map map = new HashMap<>();
        map.put("start", (page-1)*size);
        map.put("size", size);
        map.put("uid", uid);
        return skillService.selectTen(map);
    }

    private int getSkillCounts(int uid){
        return skillService.getSkillCounts(uid);
    }
}
